FROM  swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/library/debian:12 AS builder

RUN rm -rf /etc/apt/sources.list /etc/apt/sources.list.d/
COPY ./sources.list /etc/apt/sources.list
COPY ./requirements.txt .
RUN apt update && \
    apt install python3 python3-dev python3-pip default-libmysqlclient-dev \
    pkg-config  build-essential supervisor \
    gcc libmariadb-dev -y && \
    apt clean && \
    pip install --upgrade  pip -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com --break-system-packages && \
    pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com --break-system-packages

FROM  swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/library/debian:12
LABEL authors="kdly"
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PATH=/root/.local/bin:/usr/local/bin:/usr/bin:$PATH

COPY ./sources.list /etc/apt/sources.list

RUN rm -rf /etc/apt/sources.list.d && \
    apt update && \
    apt install supervisor -y && \
    apt clean all

COPY --from=builder /usr/bin /usr/bin
COPY --from=builder /usr/local/bin /usr/local/bin
COPY --from=builder /usr/local/lib/python3.11 /usr/local/lib/python3.11
COPY --from=builder /usr/lib/x86_64-linux-gnu/libexpat.so.1 /usr/lib/x86_64-linux-gnu/
COPY --from=builder /usr/lib/x86_64-linux-gnu/libmariadb.so.3 /usr/lib/x86_64-linux-gnu/
COPY --from=builder /usr/lib/x86_64-linux-gnu/libmariadb3/ /usr/lib/x86_64-linux-gnu/libmariadb3/

WORKDIR /app

COPY . .

# 创建非root用户
RUN python3 manage.py collectstatic && \
    sed -ri 's/self.check_database_version_supported()//g' /usr/local/lib/python3.11/dist-packages/django/db/backends/base/base.py && \
    mkdir -p /app/log/supervisor && \
    useradd appuser && \
    chown -R appuser:appuser /app

USER appuser

EXPOSE 8000

CMD ["supervisord", "-n", "-c", "/app/supervisord.conf"]
